<?php

/*

Copyright (c) 2007 Daylife, Inc.

Provides logic for user to update configuration, persisted in XML.

*/

include_once('../common.php');

class Topic_Builder {

  protected $_user_ID;
  
  
  
  protected $_config_dir = "./config";

  protected $_config_file;

  // obj SimpleXMLElement representing latest stored config
  protected $_stored_config;

  // array of new config data
  protected $_new_config;


  /*

  void __construct()

  mixed $user_ID

  array $config -- New config data.

  bool $preview -- The user has elected to preview their changes.

  */

  public function __construct( $user_ID = NULL, $config = array(), $preview = FALSE ) {

    
    try {

      if ( ! $user_ID ) {

        throw new Exception( "user_ID undefined." );

      }
      // if

    }
    // try

    catch ( Exception $exception ) {

      $this->_handle_exception( $exception );

    }
    // catch

    $this->_user_ID = $user_ID;

    $this->_new_config = $config;


    $default_config_file = "/var/www/endeepen/tpb/topic_builder/config/default.xml";

    if ( strlen( $user_ID ) ) {

      // Assumes $user_ID is sanitized
      //$config_file = "{$this->_config_dir}/{$user_ID}.xml";
      $config_file = "/var/www/endeepen/instances/{$user_ID}/instance_config.xml";

      // User has elected to preview their changes.  Everything happens the same as an update, only the name of the config file is different.

      if ( $preview ) {

        $default_config_file = $config_file;

        $config_file = "{$this->_config_dir}/{$user_ID}-preview.xml";

      }
      // if


      // User doesn't have a config file yet

      if ( ! file_exists( $config_file ) ) {

        try {

          if ( ! copy( $default_config_file, $config_file ) ) {

            throw new Exception( "Couldn't copy default config file." );

          }
          // if

          if ( ! chmod( $config_file, 0777 ) ) {

            throw new Exception( "Couldn't chmod user config file." );

          }
          // if

        }
        // try

        catch ( Exception $exception ) {

          $this->_handle_exception( $exception );

        }
        // catch

      }
      // if

    }
    // if


    $this->_config_file = $config_file;

    $this->_load_config();


    // User has submitted config data for update or preview

    if ( $this->_new_config && strlen( $this->_user_ID ) ) {

      $this->_update_config();

    }
    // if


    return;

  }
  // __construct


  /*

  void _load_config() -- Load config from XML storage.

  */

  protected function _load_config() {

    try {

      if ( ! ( $this->_stored_config = simplexml_load_file( $this->_config_file ) ) ) {

        throw new Exception( "Couldn't instantiate SimpleXMLElement object" );

      }
      // if

    }
    // try

    catch ( Exception $exception ) {

      $this->_handle_exception( $exception );

    }
    // catch


    return;

  }
  // _load_config


  /*

  void _update_config() -- Update the user config file.

  */

  protected function _update_config() {

    // Sanitize user submitted data before updating
    $this->_sanitize_new_config();

    $new_config = $this->_new_config;

    ob_start();

    include "{$this->_config_dir}/template.php";

    try {

      if ( ! ( $new_xml = ob_get_clean() ) ) {

        throw new Exception( "Config file template not loaded." );

      }
      // if


      if ( ! file_put_contents( $this->_config_file, $new_xml, ( LOCK_EX ) ) ) {

        throw new Exception( "Couldn't write config file." );

      }
      // if

    }
    // try

    catch ( Exception $exception ) {

      $this->_handle_exception( $exception );

    }
    // catch


    $this->_load_config();


    return;

  }
  // _update_config


  /*

  void _sanitize_new_config() -- Sanitize user submitted config data.

  */

  protected function _sanitize_new_config() {

    foreach ( array( 'header_html', 'footer_html' ) as $param_name ) {

      $this->_new_config[ $param_name ] = strip_tags( $this->_new_config[ $param_name ], "" );

    }
    // foreach


    return;

  }
  // _sanitize_new_config


  /*

  void _handle_exception()

  obj Exception $exception

  */

  protected function _handle_exception( $exception ) {

    // DEBUG do something here


    return;

  }
  // _handle_exception


  /*

  array process_form_submission() -- Process form submission if it has occured.

  mixed $user_ID

  */

  public static function process_form_submission( $user_ID = NULL ) {

    $config = array();

    if ( ! (

      ( $_POST[ 'sub_save' ] || $_POST[ 'sub_preview' ] ) &&

      strlen( $user_ID )

    ) ) {

      return $config;

    }
    // if


    $config = $_POST[ 'config' ];

    // Set default values (checkboxes that may not have been submitted)

    foreach ( array( 'images', 'articles', 'quotes', 'connections', 'stories', 'timeline' ) as $component ) {

      if ( is_null( $config[ 'components' ][ $component ][ 'enabled' ] ) ) {

        $config[ 'components' ][ $component ][ 'enabled' ] = 0;

      }
      // if

    }
    // foreach


    return $config;

  }
  // process_form_submission


  /*

  &obj stored_config() -- Retrieves reference to $this->_stored_config.

  */

  public function &stored_config() {

    return $this->_stored_config;

  }
  // stored_config


  /*

  string escape_xml() -- Escape data for XML output.

  mixed $param_value

  */

  protected function escape_xml( $param_value ) {

    $param_value = htmlspecialchars( $param_value );


    return $param_value;

  }
  // escape_xml

}
// Topic_Builder

?>